package com.snoob.lixing.util;

import java.security.MessageDigest;
import java.util.*;

/**
 * 自定义字符串工具类
 */
public class StrUtil {

    private static final String str = "A1a2B3b4C5c6D7d8E9e9F9f7G6g5H4h3J2j1KkLlMmNnPpQqRrSsTtUuVvWwXxYyZz";
    private static final String SALT = "gdd";

    /**
     * 36长度的UUID字符串
     */
    public static String getUuidStr() {
        return UUID.randomUUID().toString();
    }

    /**
     * 4位长度的验证码字符串,由大小写字母和数字组成
     */
    public static String getCodeStr() {
        String tempStr = "";
        for (int i = 0; i < 4; i++) {
            tempStr += str.charAt(new Random().nextInt(str.length()));
        }
        return tempStr;
    }

    /**
     * 生成md5字符串
     */
    public static String getMd5Str(String str) throws Exception {
        str = str + SALT;
        MessageDigest md5 = md5 = MessageDigest.getInstance("MD5");
        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 判断字符都是否有效
     */
    public static Boolean isEffective(String str) {
        if (str != null && str.length() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 将以逗号分隔的字符串转化成List列表 "1,2,3,4" => ["1","2","3","4"]
     */
    public static List<String> getListForCommaStr(String str) {
        return Arrays.asList(str.split(","));
    }

    /**
     * 求两个字符串数组的并集，利用set的元素唯一性
     */
    public static String[] union(String[] arr1, String[] arr2) {
        Set<String> set = new HashSet<String>();
        for (String str : arr1) {
            set.add(str);
        }
        for (String str : arr2) {
            set.add(str);
        }
        String[] result = {};
        return set.toArray(result);
    }

    /**
     * 求两个数组的交集
     */
    public static String[] intersect(String[] arr1, String[] arr2) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        LinkedList<String> list = new LinkedList<String>();
        for (String str : arr1) {
            if (!map.containsKey(str)) {
                map.put(str, Boolean.FALSE);
            }
        }
        for (String str : arr2) {
            if (map.containsKey(str)) {
                map.put(str, Boolean.TRUE);
            }
        }
        for (Map.Entry<String, Boolean> e : map.entrySet()) {
            if (e.getValue().equals(Boolean.TRUE)) {
                list.add(e.getKey());
            }
        }
        String[] result = {};
        return list.toArray(result);
    }

    /**
     * 求两个数组的差集
     */
    public static String[] minus(String[] arr1, String[] arr2) {
        LinkedList<String> list = new LinkedList<String>();
        LinkedList<String> history = new LinkedList<String>();
        String[] longerArr = arr1;
        String[] shorterArr = arr2;
        //找出较长的数组来减较短的数组
        if (arr1.length > arr2.length) {
            longerArr = arr2;
            shorterArr = arr1;
        }
        for (String str : longerArr) {
            if (!list.contains(str)) {
                list.add(str);
            }
        }
        for (String str : shorterArr) {
            if (list.contains(str)) {
                history.add(str);
                list.remove(str);
            } else {
                if (!history.contains(str)) {
                    list.add(str);
                }
            }
        }
        String[] result = {};
        return list.toArray(result);
    }

}
